home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -screenplay- / shareware / warpquake / warpquakesrc / common.c < prev    next >
C/C++ Source or Header  |  2000-02-29  |  36KB  |  1,891 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // common.c -- misc functions used in client and server
  21.  
  22. #include "quakedef.h"
  23.  
  24. #define NUM_SAFE_ARGVS  7
  25.  
  26. static char     *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1];
  27. static char     *argvdummy = " ";
  28.  
  29. static char     *safeargvs[NUM_SAFE_ARGVS] =
  30.     {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse", "-dibonly"};
  31.  
  32. cvar_t  registered = {"registered","0"};
  33. cvar_t  cmdline = {"cmdline","0", false, true};
  34.  
  35. qboolean        com_modified;   // set true if using non-id files
  36.  
  37. qboolean        proghack;
  38.  
  39. int             static_registered = 1;  // only for startup check, then set
  40.  
  41. qboolean        msg_suppress_1 = 0;
  42.  
  43. void COM_InitFilesystem (void);
  44.  
  45. // if a packfile directory differs from this, it is assumed to be hacked
  46. #define PAK0_COUNT              339
  47. #define PAK0_CRC                32981
  48.  
  49. char    com_token[1024];
  50. int        com_argc;
  51. char    **com_argv;
  52.  
  53. #define CMDLINE_LENGTH    256
  54. char    com_cmdline[CMDLINE_LENGTH];
  55.  
  56. qboolean        standard_quake = true, rogue, hipnotic;
  57.  
  58. // this graphic needs to be in the pak file to use registered features
  59. unsigned short pop[] =
  60. {
  61.  0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
  62. ,0x0000,0x0000,0x6600,0x0000,0x0000,0x0000,0x6600,0x0000
  63. ,0x0000,0x0066,0x0000,0x0000,0x0000,0x0000,0x0067,0x0000
  64. ,0x0000,0x6665,0x0000,0x0000,0x0000,0x0000,0x0065,0x6600
  65. ,0x0063,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6563
  66. ,0x0064,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6564
  67. ,0x0064,0x6564,0x0000,0x6469,0x6969,0x6400,0x0064,0x6564
  68. ,0x0063,0x6568,0x6200,0x0064,0x6864,0x0000,0x6268,0x6563
  69. ,0x0000,0x6567,0x6963,0x0064,0x6764,0x0063,0x6967,0x6500
  70. ,0x0000,0x6266,0x6769,0x6a68,0x6768,0x6a69,0x6766,0x6200
  71. ,0x0000,0x0062,0x6566,0x6666,0x6666,0x6666,0x6562,0x0000
  72. ,0x0000,0x0000,0x0062,0x6364,0x6664,0x6362,0x0000,0x0000
  73. ,0x0000,0x0000,0x0000,0x0062,0x6662,0x0000,0x0000,0x0000
  74. ,0x0000,0x0000,0x0000,0x0061,0x6661,0x0000,0x0000,0x0000
  75. ,0x0000,0x0000,0x0000,0x0000,0x6500,0x0000,0x0000,0x0000
  76. ,0x0000,0x0000,0x0000,0x0000,0x6400,0x0000,0x0000,0x0000
  77. };
  78.  
  79. /*
  80.  
  81.  
  82. All of Quake's data access is through a hierchal file system, but the
  83. contents of the file system can be transparently merged from several
  84. sources.
  85.  
  86. The "base directory" is the path to the directory holding the
  87. quake.exe and all game directories.  The sys_* files pass this to
  88. host_init in quakeparms_t->basedir.  This can be overridden with the
  89. "-basedir" command line parm to allow code debugging in a different
  90. directory.  The base directory is only used during filesystem
  91. initialization.
  92.  
  93. The "game directory" is the first tree on the search path and
  94. directory that all generated files (savegames, screenshots, demos,
  95. config files) will be saved to.  This can be overridden with the
  96. "-game" command line parameter.  The game directory can never be
  97. changed while quake is executing.  This is a precacution against
  98. having a malicious server instruct clients to write files over areas
  99. they shouldn't.
  100.  
  101. The "cache directory" is only used during development to save network
  102. bandwidth, especially over ISDN / T1 lines.  If there is a cache
  103. directory specified, when a file is found by the normal search path,
  104. it will be mirrored into the cache directory, then opened there.
  105.  
  106.  
  107.  
  108. FIXME:
  109. The file "parms.txt" will be read out of the game directory and
  110. appended to the current command line arguments to allow different
  111. games to initialize startup parms differently.  This could be used to
  112. add a "-sspeed 22050" for the high quality sound edition.  Because
  113. they are added at the end, they will not override an explicit setting
  114. on the original command line.       */
  115.  
  116. //============================================================================
  117.  
  118.  
  119. // ClearLink is used for new headnodes
  120. void ClearLink (link_t *l)
  121. {
  122.     l->prev = l->next = l;
  123. }
  124.  
  125. void RemoveLink (link_t *l)
  126. {
  127.     l->next->prev = l->prev;
  128.     l->prev->next = l->next;
  129. }
  130.  
  131. void InsertLinkBefore (link_t *l, link_t *before)
  132. {
  133.     l->next = before;
  134.     l->prev = before->prev;
  135.     l->prev->next = l;
  136.     l->next->prev = l;
  137. }
  138. void InsertLinkAfter (link_t *l, link_t *after)
  139. {
  140.     l->next = after->next;
  141.     l->prev = after;
  142.     l->prev->next = l;
  143.     l->next->prev = l;
  144. }
  145.  
  146. /*
  147. ============================================================================
  148.  
  149.                     LIBRARY REPLACEMENT FUNCTIONS
  150.  
  151. ============================================================================
  152. */
  153.  
  154. void Q_memset (void *dest, int fill, int count)
  155. {
  156.     int             i;
  157.     
  158.     if ( (((long)dest | count) & 3) == 0)
  159.     {
  160.         count >>= 2;
  161.         fill = fill | (fill<<8) | (fill<<16) | (fill<<24);
  162.         for (i=0 ; i<count ; i++)
  163.             ((int *)dest)[i] = fill;
  164.     }
  165.     else
  166.         for (i=0 ; i<count ; i++)
  167.             ((byte *)dest)[i] = fill;
  168. }
  169.  
  170. void Q_memcpy (void *dest, void *src, int count)
  171. {
  172.     int             i;
  173.     
  174.     if (( ( (long)dest | (long)src | count) & 3) == 0 )
  175.     {
  176.         count>>=2;
  177.         for (i=0 ; i<count ; i++)
  178.             ((int *)dest)[i] = ((int *)src)[i];
  179.     }
  180.     else
  181.         for (i=0 ; i<count ; i++)
  182.             ((byte *)dest)[i] = ((byte *)src)[i];
  183. }
  184.  
  185. int Q_memcmp (void *m1, void *m2, int count)
  186. {
  187.     while(count)
  188.     {
  189.         count--;
  190.         if (((byte *)m1)[count] != ((byte *)m2)[count])
  191.             return -1;
  192.     }
  193.     return 0;
  194. }
  195.  
  196. void Q_strcpy (char *dest, char *src)
  197. {
  198.     while (*src)
  199.     {
  200.         *dest++ = *src++;
  201.     }
  202.     *dest++ = 0;
  203. }
  204.  
  205. void Q_strncpy (char *dest, char *src, int count)
  206. {
  207.     while (*src && count--)
  208.     {
  209.         *dest++ = *src++;
  210.     }
  211.     if (count)
  212.         *dest++ = 0;
  213. }
  214.  
  215. int Q_strlen (char *str)
  216. {
  217.     int             count;
  218.     
  219.     count = 0;
  220.     while (str[count])
  221.         count++;
  222.  
  223.     return count;
  224. }
  225.  
  226. char *Q_strrchr(char *s, char c)
  227. {
  228.     int len = Q_strlen(s);
  229.     s += len;
  230.     while (len--)
  231.     if (*--s == c) return s;
  232.     return 0;
  233. }
  234.  
  235. void Q_strcat (char *dest, char *src)
  236. {
  237.     dest += Q_strlen(dest);
  238.     Q_strcpy (dest, src);
  239. }
  240.  
  241. int Q_strcmp (char *s1, char *s2)
  242. {
  243.     while (1)
  244.     {
  245.         if (*s1 != *s2)
  246.             return -1;              // strings not equal    
  247.         if (!*s1)
  248.             return 0;               // strings are equal
  249.         s1++;
  250.         s2++;
  251.     }
  252.     
  253.     return -1;
  254. }
  255.  
  256. int Q_strncmp (char *s1, char *s2, int count)
  257. {
  258.     while (1)
  259.     {
  260.         if (!count--)
  261.             return 0;
  262.         if (*s1 != *s2)
  263.             return -1;              // strings not equal    
  264.         if (!*s1)
  265.             return 0;               // strings are equal
  266.         s1++;
  267.         s2++;
  268.     }
  269.     
  270.     return -1;
  271. }
  272.  
  273. int Q_strncasecmp (char *s1, char *s2, int n)
  274. {
  275.     int             c1, c2;
  276.     
  277.     while (1)
  278.     {
  279.         c1 = *s1++;
  280.         c2 = *s2++;
  281.  
  282.         if (!n--)
  283.             return 0;               // strings are equal until end point
  284.         
  285.         if (c1 != c2)
  286.         {
  287.             if (c1 >= 'a' && c1 <= 'z')
  288.                 c1 -= ('a' - 'A');
  289.             if (c2 >= 'a' && c2 <= 'z')
  290.                 c2 -= ('a' - 'A');
  291.             if (c1 != c2)
  292.                 return -1;              // strings not equal
  293.         }
  294.         if (!c1)
  295.             return 0;               // strings are equal
  296. //              s1++;
  297. //              s2++;
  298.     }
  299.     
  300.     return -1;
  301. }
  302.  
  303. int Q_strcasecmp (char *s1, char *s2)
  304. {
  305.     return Q_strncasecmp (s1, s2, 99999);
  306. }
  307.  
  308. int Q_atoi (char *str)
  309. {
  310.     int             val;
  311.     int             sign;
  312.     int             c;
  313.     
  314.     if (*str == '-')
  315.     {
  316.         sign = -1;
  317.         str++;
  318.     }
  319.     else
  320.         sign = 1;
  321.         
  322.     val = 0;
  323.  
  324. //
  325. // check for hex
  326. //
  327.     if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  328.     {
  329.         str += 2;
  330.         while (1)
  331.         {
  332.             c = *str++;
  333.             if (c >= '0' && c <= '9')
  334.                 val = (val<<4) + c - '0';
  335.             else if (c >= 'a' && c <= 'f')
  336.                 val = (val<<4) + c - 'a' + 10;
  337.             else if (c >= 'A' && c <= 'F')
  338.                 val = (val<<4) + c - 'A' + 10;
  339.             else
  340.                 return val*sign;
  341.         }
  342.     }
  343.     
  344. //
  345. // check for character
  346. //
  347.     if (str[0] == '\'')
  348.     {
  349.         return sign * str[1];
  350.     }
  351.     
  352. //
  353. // assume decimal
  354. //
  355.     while (1)
  356.     {
  357.         c = *str++;
  358.         if (c <'0' || c > '9')
  359.             return val*sign;
  360.         val = val*10 + c - '0';
  361.     }
  362.     
  363.     return 0;
  364. }
  365.  
  366.  
  367. float Q_atof (char *str)
  368. {
  369.     double            val;
  370.     int             sign;
  371.     int             c;
  372.     int             decimal, total;
  373.     
  374.     if (*str == '-')
  375.     {
  376.         sign = -1;
  377.         str++;
  378.     }
  379.     else
  380.         sign = 1;
  381.         
  382.     val = 0;
  383.  
  384. //
  385. // check for hex
  386. //
  387.     if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  388.     {
  389.         str += 2;
  390.         while (1)
  391.         {
  392.             c = *str++;
  393.             if (c >= '0' && c <= '9')
  394.                 val = (val*16) + c - '0';
  395.             else if (c >= 'a' && c <= 'f')
  396.                 val = (val*16) + c - 'a' + 10;
  397.             else if (c >= 'A' && c <= 'F')
  398.                 val = (val*16) + c - 'A' + 10;
  399.             else
  400.                 return val*sign;
  401.         }
  402.     }
  403.     
  404. //
  405. // check for character
  406. //
  407.     if (str[0] == '\'')
  408.     {
  409.         return sign * str[1];
  410.     }
  411.     
  412. //
  413. // assume decimal
  414. //
  415.     decimal = -1;
  416.     total = 0;
  417.     while (1)
  418.     {
  419.         c = *str++;
  420.         if (c == '.')
  421.         {
  422.             decimal = total;
  423.             continue;
  424.         }
  425.         if (c <'0' || c > '9')
  426.             break;
  427.         val = val*10 + c - '0';
  428.         total++;
  429.     }
  430.  
  431.     if (decimal == -1)
  432.         return val*sign;
  433.     while (total > decimal)
  434.     {
  435.         val /= 10;
  436.         total--;
  437.     }
  438.     
  439.     return val*sign;
  440. }
  441.  
  442. /*
  443. ============================================================================
  444.  
  445.                     BYTE ORDER FUNCTIONS
  446.  
  447. ============================================================================
  448. */
  449.  
  450. qboolean        bigendien;
  451.  
  452. short   (*BigShort) (short l);
  453. short   (*LittleShort) (short l);
  454. int     (*BigLong) (int l);
  455. int     (*LittleLong) (int l);
  456. float   (*BigFloat) (float l);
  457. float   (*LittleFloat) (float l);
  458.  
  459. short   ShortSwap (short l)
  460. {
  461.     byte    b1,b2;
  462.  
  463.     b1 = l&255;
  464.     b2 = (l>>8)&255;
  465.  
  466.     return (b1<<8) + b2;
  467. }
  468.  
  469. short   ShortNoSwap (short l)
  470. {
  471.     return l;
  472. }
  473.  
  474. int    LongSwap (int l)
  475. {
  476.     byte    b1,b2,b3,b4;
  477.  
  478.     b1 = l&255;
  479.     b2 = (l>>8)&255;
  480.     b3 = (l>>16)&255;
  481.     b4 = (l>>24)&255;
  482.  
  483.     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  484. }
  485.  
  486. int     LongNoSwap (int l)
  487. {
  488.     return l;
  489. }
  490.  
  491. float FloatSwap (float f)
  492. {
  493.     union
  494.     {
  495.         float   f;
  496.         byte    b[4];
  497.     } dat1, dat2;
  498.     
  499.     
  500.     dat1.f = f;
  501.     dat2.b[0] = dat1.b[3];
  502.     dat2.b[1] = dat1.b[2];
  503.     dat2.b[2] = dat1.b[1];
  504.     dat2.b[3] = dat1.b[0];
  505.     return dat2.f;
  506. }
  507.  
  508. float FloatNoSwap (float f)
  509. {
  510.     return f;
  511. }
  512.  
  513. /*
  514. ==============================================================================
  515.  
  516.             MESSAGE IO FUNCTIONS
  517.  
  518. Handles byte ordering and avoids alignment errors
  519. ==============================================================================
  520. */
  521.  
  522. //
  523. // writing functions
  524. //
  525.  
  526. void MSG_WriteChar (sizebuf_t *sb, int c)
  527. {
  528.     byte    *buf;
  529.     
  530. #ifdef PARANOID
  531.     if (c < -128 || c > 127)
  532.         Sys_Error ("MSG_WriteChar: range error");
  533. #endif
  534.  
  535.     buf = SZ_GetSpace (sb, 1);
  536.     buf[0] = c;
  537. }
  538.  
  539. void MSG_WriteByte (sizebuf_t *sb, int c)
  540. {
  541.     byte    *buf;
  542.     
  543. #ifdef PARANOID
  544.     if (c < 0 || c > 255)
  545.         Sys_Error ("MSG_WriteByte: range error");
  546. #endif
  547.  
  548.     buf = SZ_GetSpace (sb, 1);
  549.     buf[0] = c;
  550. }
  551.  
  552. void MSG_WriteShort (sizebuf_t *sb, int c)
  553. {
  554.     byte    *buf;
  555.     
  556. #ifdef PARANOID
  557.     if (c < ((short)0x8000) || c > (short)0x7fff)
  558.         Sys_Error ("MSG_WriteShort: range error");
  559. #endif
  560.  
  561.     buf = SZ_GetSpace (sb, 2);
  562.     buf[0] = c&0xff;
  563.     buf[1] = c>>8;
  564. }
  565.  
  566. void MSG_WriteLong (sizebuf_t *sb, int c)
  567. {
  568.     byte    *buf;
  569.     
  570.     buf = SZ_GetSpace (sb, 4);
  571.     buf[0] = c&0xff;
  572.     buf[1] = (c>>8)&0xff;
  573.     buf[2] = (c>>16)&0xff;
  574.     buf[3] = c>>24;
  575. }
  576.  
  577. void MSG_WriteFloat (sizebuf_t *sb, float f)
  578. {
  579.     union
  580.     {
  581.         float   f;
  582.         int     l;
  583.     } dat;
  584.     
  585.     
  586.     dat.f = f;
  587.     dat.l = LittleLong (dat.l);
  588.     
  589.     SZ_Write (sb, &dat.l, 4);
  590. }
  591.  
  592. void MSG_WriteString (sizebuf_t *sb, char *s)
  593. {
  594.     if (!s)
  595.         SZ_Write (sb, "", 1);
  596.     else
  597.         SZ_Write (sb, s, Q_strlen(s)+1);
  598. }
  599.  
  600. void MSG_WriteCoord (sizebuf_t *sb, float f)
  601. {
  602.     MSG_WriteShort (sb, (int)(f*8));
  603. }
  604.  
  605. void MSG_WriteAngle (sizebuf_t *sb, float f)
  606. {
  607.     MSG_WriteByte (sb, ((int)f*256/360) & 255);
  608. }
  609.  
  610. //
  611. // reading functions
  612. //
  613. int                     msg_readcount;
  614. qboolean        msg_badread;
  615.  
  616. void MSG_BeginReading (void)
  617. {
  618.     msg_readcount = 0;
  619.     msg_badread = false;
  620. }
  621.  
  622. // returns -1 and sets msg_badread if no more characters are available
  623. int MSG_ReadChar (void)
  624. {
  625.     int     c;
  626.     
  627.     if (msg_readcount+1 > net_message.cursize)
  628.     {
  629.         msg_badread = true;
  630.         return -1;
  631.     }
  632.         
  633.     c = (signed char)net_message.data[msg_readcount];
  634.     msg_readcount++;
  635.     
  636.     return c;
  637. }
  638.  
  639. int MSG_ReadByte (void)
  640. {
  641.     int     c;
  642.     
  643.     if (msg_readcount+1 > net_message.cursize)
  644.     {
  645.         msg_badread = true;
  646.         return -1;
  647.     }
  648.         
  649.     c = (unsigned char)net_message.data[msg_readcount];
  650.     msg_readcount++;
  651.     
  652.     return c;
  653. }
  654.  
  655. int MSG_ReadShort (void)
  656. {
  657.     int     c;
  658.     
  659.     if (msg_readcount+2 > net_message.cursize)
  660.     {
  661.         msg_badread = true;
  662.         return -1;
  663.     }
  664.         
  665.     c = (short)(net_message.data[msg_readcount]
  666.     + (net_message.data[msg_readcount+1]<<8));
  667.     
  668.     msg_readcount += 2;
  669.     
  670.     return c;
  671. }
  672.  
  673. int MSG_ReadLong (void)
  674. {
  675.     int     c;
  676.     
  677.     if (msg_readcount+4 > net_message.cursize)
  678.     {
  679.         msg_badread = true;
  680.         return -1;
  681.     }
  682.         
  683.     c = net_message.data[msg_readcount]
  684.     + (net_message.data[msg_readcount+1]<<8)
  685.     + (net_message.data[msg_readcount+2]<<16)
  686.     + (net_message.data[msg_readcount+3]<<24);
  687.     
  688.     msg_readcount += 4;
  689.     
  690.     return c;
  691. }
  692.  
  693. float MSG_ReadFloat (void)
  694. {
  695.     union
  696.     {
  697.         byte    b[4];
  698.         float   f;
  699.         int     l;
  700.     } dat;
  701.     
  702.     dat.b[0] =      net_message.data[msg_readcount];
  703.     dat.b[1] =      net_message.data[msg_readcount+1];
  704.     dat.b[2] =      net_message.data[msg_readcount+2];
  705.     dat.b[3] =      net_message.data[msg_readcount+3];
  706.     msg_readcount += 4;
  707.     
  708.     dat.l = LittleLong (dat.l);
  709.  
  710.     return dat.f;   
  711. }
  712.  
  713. char *MSG_ReadString (void)
  714. {
  715.     static char     string[2048];
  716.     int             l,c;
  717.     
  718.     l = 0;
  719.     do
  720.     {
  721.         c = MSG_ReadChar ();
  722.         if (c == -1 || c == 0)
  723.             break;
  724.         string[l] = c;
  725.         l++;
  726.     } while (l < sizeof(string)-1);
  727.     
  728.     string[l] = 0;
  729.     
  730.     return string;
  731. }
  732.  
  733. float MSG_ReadCoord (void)
  734. {
  735.     return MSG_ReadShort() * (1.0/8);
  736. }
  737.  
  738. float MSG_ReadAngle (void)
  739. {
  740.     return MSG_ReadChar() * (360.0/256);
  741. }
  742.  
  743.  
  744.  
  745. //===========================================================================
  746.  
  747. void SZ_Alloc (sizebuf_t *buf, int startsize)
  748. {
  749.     if (startsize < 256)
  750.         startsize = 256;
  751.     buf->data = Hunk_AllocName (startsize, "sizebuf");
  752.     buf->maxsize = startsize;
  753.     buf->cursize = 0;
  754. }
  755.  
  756.  
  757. void SZ_Free (sizebuf_t *buf)
  758. {
  759. //      Z_Free (buf->data);
  760. //      buf->data = NULL;
  761. //      buf->maxsize = 0;
  762.     buf->cursize = 0;
  763. }
  764.  
  765. void SZ_Clear (sizebuf_t *buf)
  766. {
  767.     buf->cursize = 0;
  768. }
  769.  
  770. void *SZ_GetSpace (sizebuf_t *buf, int length)
  771. {
  772.     void    *data;
  773.     
  774.     if (buf->cursize + length > buf->maxsize)
  775.     {
  776.         if (!buf->allowoverflow)
  777.             Sys_Error ("SZ_GetSpace: overflow without allowoverflow set");
  778.         
  779.         if (length > buf->maxsize)
  780.             Sys_Error ("SZ_GetSpace: %i is > full buffer size", length);
  781.             
  782.         buf->overflowed = true;
  783.         Con_Printf ("SZ_GetSpace: overflow");
  784.         SZ_Clear (buf); 
  785.     }
  786.  
  787.     data = buf->data + buf->cursize;
  788.     buf->cursize += length;
  789.     
  790.     return data;
  791. }
  792.  
  793. void SZ_Write (sizebuf_t *buf, void *data, int length)
  794. {
  795.     Q_memcpy (SZ_GetSpace(buf,length),data,length);         
  796. }
  797.  
  798. void SZ_Print (sizebuf_t *buf, char *data)
  799. {
  800.     int             len;
  801.     
  802.     len = Q_strlen(data)+1;
  803.  
  804. // byte * cast to keep VC++ happy
  805.     if (buf->data[buf->cursize-1])
  806.         Q_memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0
  807.     else
  808.         Q_memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0
  809. }
  810.  
  811.  
  812. //============================================================================
  813.  
  814.  
  815. /*
  816. ============
  817. COM_SkipPath
  818. ============
  819. */
  820. char *COM_SkipPath (char *pathname)
  821. {
  822.     char    *last;
  823.     
  824.     last = pathname;
  825.     while (*pathname)
  826.     {
  827.         if (*pathname=='/')
  828.             last = pathname+1;
  829.         pathname++;
  830.     }
  831.     return last;
  832. }
  833.  
  834. /*
  835. ============
  836. COM_StripExtension
  837. ============
  838. */
  839. void COM_StripExtension (char *in, char *out)
  840. {
  841.     while (*in && *in != '.')
  842.         *out++ = *in++;
  843.     *out = 0;
  844. }
  845.  
  846. /*
  847. ============
  848. COM_FileExtension
  849. ============
  850. */
  851. char *COM_FileExtension (char *in)
  852. {
  853.     static char exten[8];
  854.     int             i;
  855.  
  856.     while (*in && *in != '.')
  857.         in++;
  858.     if (!*in)
  859.         return "";
  860.     in++;
  861.     for (i=0 ; i<7 && *in ; i++,in++)
  862.         exten[i] = *in;
  863.     exten[i] = 0;
  864.     return exten;
  865. }
  866.  
  867. /*
  868. ============
  869. COM_FileBase
  870. ============
  871. */
  872. void COM_FileBase (char *in, char *out)
  873. {
  874.     char *s, *s2;
  875.     
  876.     s = in + strlen(in) - 1;
  877.     
  878.     while (s != in && *s != '.')
  879.         s--;
  880.     
  881.     for (s2 = s ; *s2 && *s2 != '/' ; s2--)
  882.     ;
  883.     
  884.     if (s-s2 < 2)
  885.         strcpy (out,"?model?");
  886.     else
  887.     {
  888.         s--;
  889.         strncpy (out,s2+1, s-s2);
  890.         out[s-s2] = 0;
  891.     }
  892. }
  893.  
  894.  
  895. /*
  896. ==================
  897. COM_DefaultExtension
  898. ==================
  899. */
  900. void COM_DefaultExtension (char *path, char *extension)
  901. {
  902.     char    *src;
  903. //
  904. // if path doesn't have a .EXT, append extension
  905. // (extension should include the .)
  906. //
  907.     src = path + strlen(path) - 1;
  908.  
  909.     while (*src != '/' && src != path)
  910.     {
  911.         if (*src == '.')
  912.             return;                 // it has an extension
  913.         src--;
  914.     }
  915.  
  916.     strcat (path, extension);
  917. }
  918.  
  919.  
  920. /*
  921. ==============
  922. COM_Parse
  923.  
  924. Parse a token out of a string
  925. ==============
  926. */
  927. char *COM_Parse (char *data)
  928. {
  929.     int             c;
  930.     int             len;
  931.     
  932.     len = 0;
  933.     com_token[0] = 0;
  934.     
  935.     if (!data)
  936.         return NULL;
  937.         
  938. // skip whitespace
  939. skipwhite:
  940.     while ( (c = *data) <= ' ')
  941.     {
  942.         if (c == 0)
  943.             return NULL;                    // end of file;
  944.         data++;
  945.     }
  946.     
  947. // skip // comments
  948.     if (c=='/' && data[1] == '/')
  949.     {
  950.         while (*data && *data != '\n')
  951.             data++;
  952.         goto skipwhite;
  953.     }
  954.     
  955.  
  956. // handle quoted strings specially
  957.     if (c == '\"')
  958.     {
  959.         data++;
  960.         while (1)
  961.         {
  962.             c = *data++;
  963.             if (c=='\"' || !c)
  964.             {
  965.                 com_token[len] = 0;
  966.                 return data;
  967.             }
  968.             com_token[len] = c;
  969.             len++;
  970.         }
  971.     }
  972.  
  973. // parse single characters
  974.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  975.     {
  976.         com_token[len] = c;
  977.         len++;
  978.         com_token[len] = 0;
  979.         return data+1;
  980.     }
  981.  
  982. // parse a regular word
  983.     do
  984.     {
  985.         com_token[len] = c;
  986.         data++;
  987.         len++;
  988.         c = *data;
  989.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  990.             break;
  991.     } while (c>32);
  992.     
  993.     com_token[len] = 0;
  994.     return data;
  995. }
  996.  
  997.  
  998. /*
  999. ================
  1000. COM_CheckParm
  1001.  
  1002. Returns the position (1 to argc-1) in the program's argument list
  1003. where the given parameter apears, or 0 if not present
  1004. ================
  1005. */
  1006. int COM_CheckParm (char *parm)
  1007. {
  1008.     int             i;
  1009.     
  1010.     for (i=1 ; i<com_argc ; i++)
  1011.     {
  1012.         if (!com_argv[i])
  1013.             continue;               // NEXTSTEP sometimes clears appkit vars.
  1014.         if (!Q_strcmp (parm,com_argv[i]))
  1015.             return i;
  1016.     }
  1017.         
  1018.     return 0;
  1019. }
  1020.  
  1021. /*
  1022. ================
  1023. COM_CheckRegistered
  1024.  
  1025. Looks for the pop.txt file and verifies it.
  1026. Sets the "registered" cvar.
  1027. Immediately exits out if an alternate game was attempted to be started without
  1028. being registered.
  1029. ================
  1030. */
  1031. void COM_CheckRegistered (void)
  1032. {
  1033.     int             h;
  1034.     unsigned short  check[128];
  1035.     int                     i;
  1036.  
  1037.     COM_OpenFile("gfx/pop.lmp", &h);
  1038.     static_registered = 0;
  1039.  
  1040.     if (h == -1)
  1041.     {
  1042. #if WINDED
  1043.     Sys_Error ("This dedicated server requires a full registered copy of Quake");
  1044. #endif
  1045.         Con_Printf ("Playing shareware version.\n");
  1046.         if (com_modified)
  1047.             Sys_Error ("You must have the registered version to use modified games");
  1048.         return;
  1049.     }
  1050.  
  1051.     Sys_FileRead (h, check, sizeof(check));
  1052.     COM_CloseFile (h);
  1053.     
  1054.     for (i=0 ; i<128 ; i++)
  1055.         if (pop[i] != (unsigned short)BigShort (check[i]))
  1056.             Sys_Error ("Corrupted data file.");
  1057.     
  1058.     Cvar_Set ("cmdline", com_cmdline);
  1059.     Cvar_Set ("registered", "1");
  1060.     static_registered = 1;
  1061.     Con_Printf ("Playing registered version.\n");
  1062. }
  1063.  
  1064.  
  1065. void COM_Path_f (void);
  1066.  
  1067.  
  1068. /*
  1069. ================
  1070. COM_InitArgv
  1071. ================
  1072. */
  1073. void COM_InitArgv (int argc, char **argv)
  1074. {
  1075.     qboolean        safe;
  1076.     int             i, j, n;
  1077.  
  1078. // reconstitute the command line for the cmdline externally visible cvar
  1079.     n = 0;
  1080.  
  1081.     for (j=0 ; (j<MAX_NUM_ARGVS) && (j< argc) ; j++)
  1082.     {
  1083.         i = 0;
  1084.  
  1085.         while ((n < (CMDLINE_LENGTH - 1)) && argv[j][i])
  1086.         {
  1087.             com_cmdline[n++] = argv[j][i++];
  1088.         }
  1089.  
  1090.         if (n < (CMDLINE_LENGTH - 1))
  1091.             com_cmdline[n++] = ' ';
  1092.         else
  1093.             break;
  1094.     }
  1095.  
  1096.     com_cmdline[n] = 0;
  1097.  
  1098.     safe = false;
  1099.  
  1100.     for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
  1101.          com_argc++)
  1102.     {
  1103.         largv[com_argc] = argv[com_argc];
  1104.         if (!Q_strcmp ("-safe", argv[com_argc]))
  1105.             safe = true;
  1106.     }
  1107.  
  1108.     if (safe)
  1109.     {
  1110.     // force all the safe-mode switches. Note that we reserved extra space in
  1111.     // case we need to add these, so we don't need an overflow check
  1112.         for (i=0 ; i<NUM_SAFE_ARGVS ; i++)
  1113.         {
  1114.             largv[com_argc] = safeargvs[i];
  1115.             com_argc++;
  1116.         }
  1117.     }
  1118.  
  1119.     largv[com_argc] = argvdummy;
  1120.     com_argv = largv;
  1121.  
  1122.     if (COM_CheckParm ("-rogue"))
  1123.     {
  1124.         rogue = true;
  1125.         standard_quake = false;
  1126.     }
  1127.  
  1128.     if (COM_CheckParm ("-hipnotic"))
  1129.     {
  1130.         hipnotic = true;
  1131.         standard_quake = false;
  1132.     }
  1133. }
  1134.  
  1135.  
  1136. /*
  1137. ================
  1138. COM_Init
  1139. ================
  1140. */
  1141. void COM_Init (char *basedir)
  1142. {
  1143.     byte    swaptest[2] = {1,0};
  1144.  
  1145. // set the byte swapping variables in a portable manner 
  1146.     if ( *(short *)swaptest == 1)
  1147.     {
  1148.         bigendien = false;
  1149.         BigShort = ShortSwap;
  1150.         LittleShort = ShortNoSwap;
  1151.         BigLong = LongSwap;
  1152.         LittleLong = LongNoSwap;
  1153.         BigFloat = FloatSwap;
  1154.         LittleFloat = FloatNoSwap;
  1155.     }
  1156.     else
  1157.     {
  1158.         bigendien = true;
  1159.         BigShort = ShortNoSwap;
  1160.         LittleShort = ShortSwap;
  1161.         BigLong = LongNoSwap;
  1162.         LittleLong = LongSwap;
  1163.         BigFloat = FloatNoSwap;
  1164.         LittleFloat = FloatSwap;
  1165.     }
  1166.  
  1167.     Cvar_RegisterVariable (®istered);
  1168.     Cvar_RegisterVariable (&cmdline);
  1169.     Cmd_AddCommand ("path", COM_Path_f);
  1170.  
  1171.     COM_InitFilesystem ();
  1172.     COM_CheckRegistered ();
  1173. }
  1174.  
  1175.  
  1176. /*
  1177. ============
  1178. va
  1179.  
  1180. does a varargs printf into a temp buffer, so I don't need to have
  1181. varargs versions of all text functions.
  1182. FIXME: make this buffer size safe someday
  1183. ============
  1184. */
  1185. char    *va(char *format, ...)
  1186. {
  1187.     va_list         argptr;
  1188.     static char             string[1024];
  1189.     
  1190.     va_start (argptr, format);
  1191.     vsprintf (string, format,argptr);
  1192.     va_end (argptr);
  1193.  
  1194.     return string;  
  1195. }
  1196.  
  1197.  
  1198. /// just for debugging
  1199. int     memsearch (byte *start, int count, int search)
  1200. {
  1201.     int             i;
  1202.     
  1203.     for (i=0 ; i<count ; i++)
  1204.         if (start[i] == search)
  1205.             return i;
  1206.     return -1;
  1207. }
  1208.  
  1209. /*
  1210. =============================================================================
  1211.  
  1212. QUAKE FILESYSTEM
  1213.  
  1214. =============================================================================
  1215. */
  1216.  
  1217. int     com_filesize;
  1218.  
  1219.  
  1220. //
  1221. // in memory
  1222. //
  1223.  
  1224. typedef struct
  1225. {
  1226.     char    name[MAX_QPATH];
  1227.     int             filepos, filelen;
  1228. } packfile_t;
  1229.  
  1230. typedef struct pack_s
  1231. {
  1232.     char    filename[MAX_OSPATH];
  1233.     int             handle;
  1234.     int             numfiles;
  1235.     packfile_t      *files;
  1236. } pack_t;
  1237.  
  1238. //
  1239. // on disk
  1240. //
  1241. typedef struct
  1242. {
  1243.     char    name[56];
  1244.     int             filepos, filelen;
  1245. } dpackfile_t;
  1246.  
  1247. typedef struct
  1248. {
  1249.     char    id[4];
  1250.     int             dirofs;
  1251.     int             dirlen;
  1252. } dpackheader_t;
  1253.  
  1254. #define MAX_FILES_IN_PACK       2048
  1255.  
  1256. char    com_cachedir[MAX_OSPATH];
  1257. char    com_gamedir[MAX_OSPATH];
  1258.  
  1259. typedef struct searchpath_s
  1260. {
  1261.     char    filename[MAX_OSPATH];
  1262.     pack_t  *pack;          // only one of filename / pack will be used
  1263.     struct searchpath_s *next;
  1264. } searchpath_t;
  1265.  
  1266. searchpath_t    *com_searchpaths;
  1267.  
  1268. /*
  1269. ============
  1270. COM_Path_f
  1271.  
  1272. ============
  1273. */
  1274. void COM_Path_f (void)
  1275. {
  1276.     searchpath_t    *s;
  1277.     
  1278.     Con_Printf ("Current search path:\n");
  1279.     for (s=com_searchpaths ; s ; s=s->next)
  1280.     {
  1281.         if (s->pack)
  1282.         {
  1283.             Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
  1284.         }
  1285.         else
  1286.             Con_Printf ("%s\n", s->filename);
  1287.     }
  1288. }
  1289.  
  1290. /*
  1291. ============
  1292. COM_WriteFile
  1293.  
  1294. The filename will be prefixed by the current game directory
  1295. ============
  1296. */
  1297. void COM_WriteFile (char *filename, void *data, int len)
  1298. {
  1299.     int             handle;
  1300.     char    name[MAX_OSPATH];
  1301.     
  1302. #ifdef AMIGA
  1303.     if (strlen(com_gamedir) == 0 ||
  1304.         com_gamedir[strlen(com_gamedir)-1] == ':')
  1305.         sprintf (name, "%s%s", com_gamedir, filename);
  1306.     else
  1307.         sprintf (name, "%s/%s", com_gamedir, filename);
  1308. #else
  1309.     sprintf (name, "%s/%s", com_gamedir, filename);
  1310. #endif
  1311.  
  1312.     handle = Sys_FileOpenWrite (name);
  1313.     if (handle == -1)
  1314.     {
  1315.         Sys_Printf ("COM_WriteFile: failed on %s\n", name);
  1316.         return;
  1317.     }
  1318.     
  1319.     Sys_Printf ("COM_WriteFile: %s\n", name);
  1320.     Sys_FileWrite (handle, data, len);
  1321.     Sys_FileClose (handle);
  1322. }
  1323.  
  1324.  
  1325. /*
  1326. ============
  1327. COM_CreatePath
  1328.  
  1329. Only used for CopyFile
  1330. ============
  1331. */
  1332. void    COM_CreatePath (char *path)
  1333. {
  1334.     char    *ofs;
  1335.     
  1336.     for (ofs = path+1 ; *ofs ; ofs++)
  1337.     {
  1338.         if (*ofs == '/')
  1339.         {       // create the directory
  1340.             *ofs = 0;
  1341.             Sys_mkdir (path);
  1342.             *ofs = '/';
  1343.         }
  1344.     }
  1345. }
  1346.  
  1347.  
  1348. /*
  1349. ===========
  1350. COM_CopyFile
  1351.  
  1352. Copies a file over from the net to the local cache, creating any directories
  1353. needed.  This is for the convenience of developers using ISDN from home.
  1354. ===========
  1355. */
  1356. void COM_CopyFile (char *netpath, char *cachepath)
  1357. {
  1358.     int             in, out;
  1359.     int             remaining, count;
  1360.     char    buf[4096];
  1361.     
  1362.     remaining = Sys_FileOpenRead (netpath, &in);            
  1363.     COM_CreatePath (cachepath);     // create directories up to the cache file
  1364.     out = Sys_FileOpenWrite (cachepath);
  1365.     
  1366.     while (remaining)
  1367.     {
  1368.         if (remaining < sizeof(buf))
  1369.             count = remaining;
  1370.         else
  1371.             count = sizeof(buf);
  1372.         Sys_FileRead (in, buf, count);
  1373.         Sys_FileWrite (out, buf, count);
  1374.         remaining -= count;
  1375.     }
  1376.  
  1377.     Sys_FileClose (in);
  1378.     Sys_FileClose (out);    
  1379. }
  1380.  
  1381. /*
  1382. ===========
  1383. COM_FindFile
  1384.  
  1385. Finds the file in the search path.
  1386. Sets com_filesize and one of handle or file
  1387. ===========
  1388. */
  1389. int COM_FindFile (char *filename, int *handle, FILE **file)
  1390. {
  1391.     searchpath_t    *search;
  1392.     char            netpath[MAX_OSPATH];
  1393.     char            cachepath[MAX_OSPATH];
  1394.     pack_t          *pak;
  1395.     int                     i;
  1396.     int                     findtime, cachetime;
  1397.  
  1398.     if (file && handle)
  1399.         Sys_Error ("COM_FindFile: both handle and file set");
  1400.     if (!file && !handle)
  1401.         Sys_Error ("COM_FindFile: neither handle or file set");
  1402.         
  1403. //
  1404. // search through the path, one element at a time
  1405. //
  1406.     search = com_searchpaths;
  1407.     if (proghack)
  1408.     {    // gross hack to use quake 1 progs with quake 2 maps
  1409.         if (!strcmp(filename, "progs.dat"))
  1410.             search = search->next;
  1411.     }
  1412.  
  1413.     for ( ; search ; search = search->next)
  1414.     {
  1415.     // is the element a pak file?
  1416.         if (search->pack)
  1417.         {
  1418.         // look through all the pak file elements
  1419.             pak = search->pack;
  1420.             for (i=0 ; i<pak->numfiles ; i++)
  1421.                 if (!strcmp (pak->files[i].name, filename))
  1422.                 {       // found it!
  1423.                     Sys_Printf ("PackFile: %s : %s\n",pak->filename, filename);
  1424.                     if (handle)
  1425.                     {
  1426.                         *handle = pak->handle;
  1427.                         Sys_FileSeek (pak->handle, pak->files[i].filepos);
  1428.                     }
  1429.                     else
  1430.                     {       // open a new file on the pakfile
  1431.                         *file = fopen (pak->filename, "rb");
  1432.                         if (*file)
  1433.                             fseek (*file, pak->files[i].filepos, SEEK_SET);
  1434.                     }
  1435.                     com_filesize = pak->files[i].filelen;
  1436.                     return com_filesize;
  1437.                 }
  1438.         }
  1439.         else
  1440.         {               
  1441.     // check a file in the directory tree
  1442.             if (!static_registered)
  1443.             {       // if not a registered version, don't ever go beyond base
  1444.                 if ( strchr (filename, '/') || strchr (filename,'\\'))
  1445.                     continue;
  1446.             }
  1447.             
  1448. #ifdef AMIGA
  1449.             if (strlen(search->filename) == 0 ||
  1450.                 search->filename[strlen(search->filename)-1] == ':')
  1451.                 sprintf (netpath, "%s%s",search->filename, filename);
  1452.             else
  1453.                 sprintf (netpath, "%s/%s",search->filename, filename);
  1454. #else
  1455.             sprintf (netpath, "%s/%s",search->filename, filename);
  1456. #endif
  1457.             
  1458.             findtime = Sys_FileTime (netpath);
  1459.             if (findtime == -1)
  1460.                 continue;
  1461.                 
  1462.         // see if the file needs to be updated in the cache
  1463.             if (!com_cachedir[0])
  1464.                 strcpy (cachepath, netpath);
  1465.             else
  1466.             {    
  1467. #if defined(_WIN32)
  1468.                 if ((strlen(netpath) < 2) || (netpath[1] != ':'))
  1469.                     sprintf (cachepath,"%s%s", com_cachedir, netpath);
  1470.                 else
  1471.                     sprintf (cachepath,"%s%s", com_cachedir, netpath+2);
  1472. #else
  1473.                 sprintf (cachepath,"%s%s", com_cachedir, netpath);
  1474. #endif
  1475.  
  1476.                 cachetime = Sys_FileTime (cachepath);
  1477.             
  1478.                 if (cachetime < findtime)
  1479.                     COM_CopyFile (netpath, cachepath);
  1480.                 strcpy (netpath, cachepath);
  1481.             }    
  1482.  
  1483.             Sys_Printf ("FindFile: %s\n",netpath);
  1484.             com_filesize = Sys_FileOpenRead (netpath, &i);
  1485.             if (handle)
  1486.                 *handle = i;
  1487.             else
  1488.             {
  1489.                 Sys_FileClose (i);
  1490.                 *file = fopen (netpath, "rb");
  1491.             }
  1492.             return com_filesize;
  1493.         }
  1494.         
  1495.     }
  1496.     
  1497.     Sys_Printf ("FindFile: can't find %s\n", filename);
  1498.     
  1499.     if (handle)
  1500.         *handle = -1;
  1501.     else
  1502.         *file = NULL;
  1503.     com_filesize = -1;
  1504.     return -1;
  1505. }
  1506.  
  1507.  
  1508. /*
  1509. ===========
  1510. COM_OpenFile
  1511.  
  1512. filename never has a leading slash, but may contain directory walks
  1513. returns a handle and a length
  1514. it may actually be inside a pak file
  1515. ===========
  1516. */
  1517. int COM_OpenFile (char *filename, int *handle)
  1518. {
  1519.     return COM_FindFile (filename, handle, NULL);
  1520. }
  1521.  
  1522. /*
  1523. ===========
  1524. COM_FOpenFile
  1525.  
  1526. If the requested file is inside a packfile, a new FILE * will be opened
  1527. into the file.
  1528. ===========
  1529. */
  1530. int COM_FOpenFile (char *filename, FILE **file)
  1531. {
  1532.     return COM_FindFile (filename, NULL, file);
  1533. }
  1534.  
  1535. /*
  1536. ============
  1537. COM_CloseFile
  1538.  
  1539. If it is a pak file handle, don't really close it
  1540. ============
  1541. */
  1542. void COM_CloseFile (int h)
  1543. {
  1544.     searchpath_t    *s;
  1545.     
  1546.     for (s = com_searchpaths ; s ; s=s->next)
  1547.         if (s->pack && s->pack->handle == h)
  1548.             return;
  1549.             
  1550.     Sys_FileClose (h);
  1551. }
  1552.  
  1553.  
  1554. /*
  1555. ============
  1556. COM_LoadFile
  1557.  
  1558. Filename are reletive to the quake directory.
  1559. Allways appends a 0 byte.
  1560. ============
  1561. */
  1562. cache_user_t *loadcache;
  1563. byte    *loadbuf;
  1564. int             loadsize;
  1565. byte *COM_LoadFile (char *path, int usehunk)
  1566. {
  1567.     int             h;
  1568.     byte    *buf;
  1569.     char    base[32];
  1570.     int             len;
  1571.  
  1572.     buf = NULL;     // quiet compiler warning
  1573.  
  1574. // look for it in the filesystem or pack files
  1575.     len = COM_OpenFile (path, &h);
  1576.     if (h == -1)
  1577.         return NULL;
  1578.     
  1579. // extract the filename base name for hunk tag
  1580.     COM_FileBase (path, base);
  1581.     
  1582.     if (usehunk == 1)
  1583.         buf = Hunk_AllocName (len+1, base);
  1584.     else if (usehunk == 2)
  1585.         buf = Hunk_TempAlloc (len+1);
  1586.     else if (usehunk == 0)
  1587.         buf = Z_Malloc (len+1);
  1588.     else if (usehunk == 3)
  1589.         buf = Cache_Alloc (loadcache, len+1, base);
  1590.     else if (usehunk == 4)
  1591.     {
  1592.         if (len+1 > loadsize)
  1593.             buf = Hunk_TempAlloc (len+1);
  1594.         else
  1595.             buf = loadbuf;
  1596.     }
  1597.     else
  1598.         Sys_Error ("COM_LoadFile: bad usehunk");
  1599.  
  1600.     if (!buf)
  1601.         Sys_Error ("COM_LoadFile: not enough space for %s", path);
  1602.         
  1603.     ((byte *)buf)[len] = 0;
  1604.  
  1605.     Draw_BeginDisc ();
  1606.     Sys_FileRead (h, buf, len);                     
  1607.     COM_CloseFile (h);
  1608.     Draw_EndDisc ();
  1609.  
  1610.     return buf;
  1611. }
  1612.  
  1613. byte *COM_LoadHunkFile (char *path)
  1614. {
  1615.     return COM_LoadFile (path, 1);
  1616. }
  1617.  
  1618. byte *COM_LoadTempFile (char *path)
  1619. {
  1620.     return COM_LoadFile (path, 2);
  1621. }
  1622.  
  1623. void COM_LoadCacheFile (char *path, struct cache_user_s *cu)
  1624. {
  1625.     loadcache = cu;
  1626.     COM_LoadFile (path, 3);
  1627. }
  1628.  
  1629. // uses temp hunk if larger than bufsize
  1630. byte *COM_LoadStackFile (char *path, void *buffer, int bufsize)
  1631. {
  1632.     byte    *buf;
  1633.     
  1634.     loadbuf = (byte *)buffer;
  1635.     loadsize = bufsize;
  1636.     buf = COM_LoadFile (path, 4);
  1637.     
  1638.     return buf;
  1639. }
  1640.  
  1641. /*
  1642. =================
  1643. COM_LoadPackFile
  1644.  
  1645. Takes an explicit (not game tree related) path to a pak file.
  1646.  
  1647. Loads the header and directory, adding the files at the beginning
  1648. of the list so they override previous pack files.
  1649. =================
  1650. */
  1651. pack_t *COM_LoadPackFile (char *packfile)
  1652. {
  1653.     dpackheader_t   header;
  1654.     int                             i;
  1655.     packfile_t              *newfiles;
  1656.     int                             numpackfiles;
  1657.     pack_t                  *pack;
  1658.     int                             packhandle;
  1659.     dpackfile_t             info[MAX_FILES_IN_PACK];
  1660.     unsigned short          crc;
  1661.  
  1662.     if (Sys_FileOpenRead (packfile, &packhandle) == -1)
  1663.     {
  1664. //              Con_Printf ("Couldn't open %s\n", packfile);
  1665.         return NULL;
  1666.     }
  1667.     Sys_FileRead (packhandle, (void *)&header, sizeof(header));
  1668.     if (header.id[0] != 'P' || header.id[1] != 'A'
  1669.     || header.id[2] != 'C' || header.id[3] != 'K')
  1670.         Sys_Error ("%s is not a packfile", packfile);
  1671.     header.dirofs = LittleLong (header.dirofs);
  1672.     header.dirlen = LittleLong (header.dirlen);
  1673.  
  1674.     numpackfiles = header.dirlen / sizeof(dpackfile_t);
  1675.  
  1676.     if (numpackfiles > MAX_FILES_IN_PACK)
  1677.         Sys_Error ("%s has %i files", packfile, numpackfiles);
  1678.  
  1679.     if (numpackfiles != PAK0_COUNT)
  1680.         com_modified = true;    // not the original file
  1681.  
  1682.     newfiles = Hunk_AllocName (numpackfiles * sizeof(packfile_t), "packfile");
  1683.  
  1684.     Sys_FileSeek (packhandle, header.dirofs);
  1685.     Sys_FileRead (packhandle, (void *)info, header.dirlen);
  1686.  
  1687. // crc the directory to check for modifications
  1688.     CRC_Init (&crc);
  1689.     for (i=0 ; i<header.dirlen ; i++)
  1690.         CRC_ProcessByte (&crc, ((byte *)info)[i]);
  1691.     if (crc != PAK0_CRC)
  1692.         com_modified = true;
  1693.  
  1694. // parse the directory
  1695.     for (i=0 ; i<numpackfiles ; i++)
  1696.     {
  1697.         strcpy (newfiles[i].name, info[i].name);
  1698.         newfiles[i].filepos = LittleLong(info[i].filepos);
  1699.         newfiles[i].filelen = LittleLong(info[i].filelen);
  1700.     }
  1701.  
  1702.     pack = Hunk_Alloc (sizeof (pack_t));
  1703.     strcpy (pack->filename, packfile);
  1704.     pack->handle = packhandle;
  1705.     pack->numfiles = numpackfiles;
  1706.     pack->files = newfiles;
  1707.     
  1708.     Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
  1709.     return pack;
  1710. }
  1711.  
  1712.  
  1713. /*
  1714. ================
  1715. COM_AddGameDirectory
  1716.  
  1717. Sets com_gamedir, adds the directory to the head of the path,
  1718. then loads and adds pak1.pak pak2.pak ... 
  1719. ================
  1720. */
  1721. void COM_AddGameDirectory (char *dir)
  1722. {
  1723.     int                             i;
  1724.     searchpath_t    *search;
  1725.     pack_t                  *pak;
  1726.     char                    pakfile[MAX_OSPATH];
  1727.  
  1728.     strcpy (com_gamedir, dir);
  1729.  
  1730. //
  1731. // add the directory to the search path
  1732. //
  1733.     search = Hunk_Alloc (sizeof(searchpath_t));
  1734.     strcpy (search->filename, dir);
  1735.     search->next = com_searchpaths;
  1736.     com_searchpaths = search;
  1737.  
  1738. //
  1739. // add any pak files in the format pak0.pak pak1.pak, ...
  1740. //
  1741.     for (i=0 ; ; i++)
  1742.     {
  1743. #ifdef AMIGA
  1744.         if (strlen(dir) == 0 ||
  1745.             dir[strlen(dir)-1] == ':')
  1746.             sprintf (pakfile, "%spak%i.pak", dir, i);
  1747.         else
  1748.             sprintf (pakfile, "%s/pak%i.pak", dir, i);
  1749. #else
  1750.         sprintf (pakfile, "%s/pak%i.pak", dir, i);
  1751. #endif
  1752.         pak = COM_LoadPackFile (pakfile);
  1753.         if (!pak)
  1754.             break;
  1755.         search = Hunk_Alloc (sizeof(searchpath_t));
  1756.         search->pack = pak;
  1757.         search->next = com_searchpaths;
  1758.         com_searchpaths = search;               
  1759.     }
  1760.  
  1761. //
  1762. // add the contents of the parms.txt file to the end of the command line
  1763. //
  1764.  
  1765. }
  1766.  
  1767. /*
  1768. ================
  1769. COM_InitFilesystem
  1770. ================
  1771. */
  1772. void COM_InitFilesystem (void)
  1773. {
  1774.     int             i, j;
  1775.     char    basedir[MAX_OSPATH];
  1776.     searchpath_t    *search;
  1777.  
  1778. //
  1779. // -basedir <path>
  1780. // Overrides the system supplied base directory (under GAMENAME)
  1781. //
  1782.     i = COM_CheckParm ("-basedir");
  1783.     if (i && i < com_argc-1)
  1784.         strcpy (basedir, com_argv[i+1]);
  1785.     else
  1786.         strcpy (basedir, host_parms.basedir);
  1787.  
  1788.     j = strlen (basedir);
  1789.  
  1790.     if (j > 0)
  1791.     {
  1792.         if ((basedir[j-1] == '\\') || (basedir[j-1] == '/'))
  1793.             basedir[j-1] = 0;
  1794.     }
  1795.  
  1796. //
  1797. // -cachedir <path>
  1798. // Overrides the system supplied cache directory (NULL or /qcache)
  1799. // -cachedir - will disable caching.
  1800. //
  1801.     i = COM_CheckParm ("-cachedir");
  1802.     if (i && i < com_argc-1)
  1803.     {
  1804.         if (com_argv[i+1][0] == '-')
  1805.             com_cachedir[0] = 0;
  1806.         else
  1807.             strcpy (com_cachedir, com_argv[i+1]);
  1808.     }
  1809.     else if (host_parms.cachedir)
  1810.         strcpy (com_cachedir, host_parms.cachedir);
  1811.     else
  1812.         com_cachedir[0] = 0;
  1813.  
  1814. //
  1815. // start up with GAMENAME by default (id1)
  1816. //
  1817. #ifdef AMIGA
  1818.     if (strlen(basedir) == 0 ||
  1819.         basedir[strlen(basedir)-1] == ':') {
  1820.         COM_AddGameDirectory (va("%sid1", basedir) );
  1821.         if (COM_CheckParm ("-rogue"))
  1822.             COM_AddGameDirectory (va("%srogue", basedir) );
  1823.         if (COM_CheckParm ("-hipnotic"))
  1824.             COM_AddGameDirectory (va("%shipnotic", basedir) );
  1825.     } else {
  1826.         COM_AddGameDirectory (va("%s/id1", basedir) );
  1827.         if (COM_CheckParm ("-rogue"))
  1828.             COM_AddGameDirectory (va("%s/rogue", basedir) );
  1829.         if (COM_CheckParm ("-hipnotic"))
  1830.             COM_AddGameDirectory (va("%s/hipnotic", basedir) );
  1831.     }
  1832. #else
  1833.     COM_AddGameDirectory (va("%s/id1", basedir) );
  1834.  
  1835.     if (COM_CheckParm ("-rogue"))
  1836.         COM_AddGameDirectory (va("%s/rogue", basedir) );
  1837.     if (COM_CheckParm ("-hipnotic"))
  1838.         COM_AddGameDirectory (va("%s/hipnotic", basedir) );
  1839. #endif
  1840.  
  1841. //
  1842. // -game <gamedir>
  1843. // Adds basedir/gamedir as an override game
  1844. //
  1845.     i = COM_CheckParm ("-game");
  1846.     if (i && i < com_argc-1)
  1847.     {
  1848.         com_modified = true;
  1849. #ifdef AMIGA
  1850.         if (strlen(basedir) == 0 ||
  1851.             basedir[strlen(basedir)-1] == ':')
  1852.             COM_AddGameDirectory (va("%s%s", basedir, com_argv[i+1]));
  1853.         else
  1854.             COM_AddGameDirectory (va("%s/%s", basedir, com_argv[i+1]));
  1855. #else
  1856.         COM_AddGameDirectory (va("%s/%s", basedir, com_argv[i+1]));
  1857. #endif
  1858.     }
  1859.  
  1860. //
  1861. // -path <dir or packfile> [<dir or packfile>] ...
  1862. // Fully specifies the exact serach path, overriding the generated one
  1863. //
  1864.     i = COM_CheckParm ("-path");
  1865.     if (i)
  1866.     {
  1867.         com_modified = true;
  1868.         com_searchpaths = NULL;
  1869.         while (++i < com_argc)
  1870.         {
  1871.             if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-')
  1872.                 break;
  1873.             
  1874.             search = Hunk_Alloc (sizeof(searchpath_t));
  1875.             if ( !strcmp(COM_FileExtension(com_argv[i]), "pak") )
  1876.             {
  1877.                 search->pack = COM_LoadPackFile (com_argv[i]);
  1878.                 if (!search->pack)
  1879.                     Sys_Error ("Couldn't load packfile: %s", com_argv[i]);
  1880.             }
  1881.             else
  1882.                 strcpy (search->filename, com_argv[i]);
  1883.             search->next = com_searchpaths;
  1884.             com_searchpaths = search;
  1885.         }
  1886.     }
  1887.  
  1888.     if (COM_CheckParm ("-proghack"))
  1889.         proghack = true;
  1890. }
  1891.